home *** CD-ROM | disk | FTP | other *** search
- Path: news.telalink.net!news
- From: daver@nashville.net (David Rawle)
- Newsgroups: comp.lang.c++
- Subject: Re: Global Variables
- Date: 14 Feb 1996 06:00:53 GMT
- Organization: Telalink Corporation, Nashville, TN, USA
- Message-ID: <4frtql$73t@adam.telalink.net>
- References: <4fefbm$dhf@tofu.alt.net>
- NNTP-Posting-Host: nash-pm1-a25.telalink.net
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <4fefbm$dhf@tofu.alt.net>, ljbuller@mail.btigate.com says...
- >
- >Help!!! I'm using visualc++ 4.0. What I want to do is recall information
- >from an ini file... I already know how to do that. What I can't seem to
- >figure out is how to reference global variables in other classes. For
- >instance I can declare a variable outside of functions and class and That
- >is supposed to make it global. If I delcare x in 1.h and define it in
- >1.cpp then try to refer to x in 2.cpp I get an undeclared variable error.
- >If I make sure the declaration also exists in the 2.h, then I get a
- linker
- >error. I know this has got to be a real simple answer, but I'll be
- darned
- >if I can find the answer. Any body got help for this soul lost in c++
- >hell? Larry Buller
- >
-
- You don't state what the linker error is, but I would guess it is
- something like x in module "..." is also defined in module "...".
- You should never define global variables in .h files. You might
- declare them such as:
-
- 1.h:
- ...
- extern MyType x;
- ...
-
- and then in some file such as globals.cpp
-
- ...
- MyType x;
- ...
-
- The extern allows the code to compile and later the linker resolves
- the extern reference to the variable defined in the .cpp. Without
- the extern you are trying to create two separate variables with the
- same name.
-
- You might want to check out the K&R book on C programming.
-
- If I have misdiagnosed your problem, sorry.
-
- Daver
-
-